using System;
using com.petiteappli.xenocide;
using Mogre;
using System.Text;

namespace com.petiteappli.xenocide
{
    public class LightTower:IDisposable
    {
        Light mLight;
        float mYaw;
        MobileActor mParent;
        SceneNode mSceneNode;
        String mName;
        float mRange;
        float mCycle;
        float mDirection;
        float mAmplitude;

        public LightTower(MobileActor iParent, String iName,float iRange, float iHeight)
        {
            mParent = iParent;
            mYaw = 0.0f;
            mCycle = 0.0f;
            mDirection = Mogre.Math.PI;
            mAmplitude = 1.0f;
            mName = iName;
            mRange = iRange;
            mLight=mParent.ContainingWorld.MainWindow.SceneMgr.CreateLight(mName + "_Light1");
            mLight.Type = Light.LightTypes.LT_SPOTLIGHT;
            mLight.DiffuseColour = ColourValue.Green;

            
            //mParent.ContainingWorld.MainWindow.log("mLight :" + mLight.AttenuationRange + " / " + mLight.AttenuationConstant + " / " + mLight.AttenuationLinear + " / " + mLight.AttenuationQuadric);
            
            mLight.SetAttenuation(mRange, 0.0001f,0.001f, 0.0000004f);
            //mParent.ContainingWorld.MainWindow.log("mLight :" + mLight.AttenuationRange + " / " + mLight.AttenuationConstant + " / " + mLight.AttenuationLinear + " / " + mLight.AttenuationQuadric);
            mLight.SetSpotlightRange(new Degree(30), new Degree(45),1.0f);
            
            Quaternion q=new Quaternion();
            q.FromAngleAxis(new Radian(new Degree(00)), Vector3.NEGATIVE_UNIT_Z);
            mLight.Direction = q*Vector3.UNIT_X;
            

            mSceneNode = mParent.node.CreateChildSceneNode();
            mSceneNode.InheritScale = false;
            mSceneNode.Translate(1.0f, iHeight, 0);
            //mSceneNode2=mSceneNode.CreateChildSceneNode();
            //mSceneNode2.Translate(0.0f, iHeight, 0);
            
            //mSceneNode.Yaw(new Degree(20));
            mSceneNode.AttachObject(mLight);

        }

        public void setDirAmpl(Vector3 iDest)
        {
            float minLength = 20.0f;
            
            iDest.y=mParent.Position.y;
            Vector3 src = mParent.Orientation * Vector3.UNIT_X;
            Vector3 iDiff= iDest - mParent.Position;
            float length = iDiff.Length;
            if (length < 10.0f) length=10.0f;
            
            float dp=src.DotProduct(iDiff);
            Vector3 right = src.CrossProduct(Vector3.NEGATIVE_UNIT_Y);

            float th = Mogre.Math.ACos(dp / length).ValueRadians;

            mDirection =  (right.DotProduct(iDiff)<0)? -th:th;

            mAmplitude = 3.14159f/ (length*length) * minLength*minLength;
        }

        public void multQuadratic(float x)
        {
            mLight.SetAttenuation(mRange, mLight.AttenuationConstant, mLight.AttenuationLinear, mLight.AttenuationQuadric * x);
            mParent.ContainingWorld.MainWindow.log("mLight :" + mLight.AttenuationRange + " / " + mLight.AttenuationConstant + " / " + mLight.AttenuationLinear + " / " + mLight.AttenuationQuadric);
           
        }
        public void multConstant(float x)
        {
            mLight.SetAttenuation(mRange,mLight.AttenuationConstant*x, mLight.AttenuationLinear, mLight.AttenuationQuadric * x);
            mParent.ContainingWorld.MainWindow.log("mLight :" + mLight.AttenuationRange + " / " + mLight.AttenuationConstant + " / " + mLight.AttenuationLinear + " / " + mLight.AttenuationQuadric);
           
        }
        public void doTimer(float dt)
        {
            mCycle += dt;
            mCycle %= Mogre.Math.PI * 2;
            //mYaw += dt * 2;
            mYaw = mDirection + Mogre.Math.Cos(mCycle) * mAmplitude;

            //if (mYaw >= 360.0f) mYaw -= 360.0f;
            Quaternion q = new Quaternion(mYaw, Vector3.UNIT_Y);
            mSceneNode.Orientation = q;
            
        }
        public void Dispose()
        {
            mParent.ContainingWorld.MainWindow.SceneMgr.DestroyLight(mLight);
            mLight.Dispose();
            mParent.ContainingWorld.MainWindow.SceneMgr.DestroySceneNode(mSceneNode.Name);
            mSceneNode.Dispose();
        }

    }
   
}
